﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Xml;

namespace Mozlite.Configuration
{
    /// <summary>
    /// 提供者属性。
    /// </summary>
    public class Provider
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Configuration.Provider"/>。
        /// </summary>
        /// <param name="attributes">节点属性集合。</param>
        /// <param name="owner">数据库所有者。</param>
        /// <param name="profix">表格前缀。</param>
        /// <param name="connectionString">链接字符串。</param>
        public Provider(XmlAttributeCollection attributes, string owner, string profix, string connectionString)
        {
            foreach (XmlAttribute attr in attributes)
            {
                this.attributes.Add(attr.Name, attr.Value);
            }
            this.Name = this.attributes["name"];
            this.Type = this.attributes["type"];
            this.DbOwner = this.attributes["owner"] ?? owner;
            if (!string.IsNullOrEmpty(this.DbOwner))
                this.DbOwner += ".";
            this.DbProfix = this.attributes["profix"] ?? profix;
            if (!string.IsNullOrEmpty(this.DbProfix))
                this.DbProfix += "_";
            this.ConnectionString = this.attributes["connectionString"] ?? connectionString;
        }
        private NameValueCollection attributes = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
        /// <summary>
        /// 数据数据库提供者的名称。
        /// </summary>
        public string Name { get; private set; }
        /// <summary>
        /// 获取数据库提供者类型。
        /// </summary>
        public string Type { get; private set; }
        /// <summary>
        /// 获取链接字符串。
        /// </summary>
        public string ConnectionString { get; private set; }
        /// <summary>
        /// 获取属性。
        /// </summary>
        /// <param name="name">属性名称。</param>
        /// <returns>返回当前属性值。</returns>
        public string this[string name] {
            get {
                return this.attributes[name];
            }
        }
        /// <summary>
        /// 数据数据库提供者的名称。
        /// </summary>
        public string DbOwner { get; private set; }
        /// <summary>
        /// 获取数据库提供者类型。
        /// </summary>
        public string DbProfix { get; private set; }
    }
}
